import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { addRole, delRole, getRole, listRole, updateRole } from '@/api/system/role'
export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'roleId'
  const moduleName =  '角色'
  const apiFn = {
    listApiFn: listRole,
    delApiFn: delRole,
    apiFnConfig:{
      info: getRole,
      add: addRole,
      edit: updateRole
    }
  }

  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
  }

}


/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_normal_disable',
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: '角色名称',
      prop: 'roleName',
      tableColumnProps: {
        align: 'center'
      },
      order: 10
    },
    {
      label: '权限字符',
      prop: 'roleKey',
      tooltip: isForm ? '控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasRole(\'admin\')`)' :"",
      tableColumnProps: {
        align: 'center'
      },
      order: 9
    },
    {
      label: '角色顺序',
      prop: 'roleSort',
      hideInSearch: true,
      hideInTable: true,
      valueType: 'input-number',
      fieldProps: {
        controlsPosition:'right'
      }
    },
    {
      label: '状态',
      prop: 'status',
      tableColumnProps: {
        align: 'center'
      },
      valueType: 'select',
      options: dictRawData.sys_normal_disable,
      propInForm:{
        valueType: 'radio',
      },
      order: 8
    },
    {
      label: '菜单权限',
      prop: 'menuScope',
      hideInSearch: true,
      hideInTable: true,
    },
    {
      label: '备注',
      prop: 'remark',
      hideInSearch: true,
      hideInTable: true,
      valueType:'textarea'
    },
    {
      label: '创建时间',
      prop: 'createTime',
      tableColumnProps: {
        align: 'center'
      },
      order: 6,
      hideInForm: true,
      valueType:'date-picker',
      fieldProps: {
        type: 'daterange',
        valueFormat: "YYYY-MM-DD"
      },
    },
    {
      label: '操作',
      prop: 'action',
      width: '160px',
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: 'center',
        fixed:'right'
      }
    },
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

